package edu.univercidade.tcc.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.roo.addon.web.mvc.controller.RooWebScaffold;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import edu.univercidade.tcc.server.model.Issue;

@RooWebScaffold(path = "issues", formBackingObject = Issue.class, delete = false)
@RequestMapping("/issues")
@Controller
public class IssueController {

	@RequestMapping(method = RequestMethod.POST)
	public String create(@Valid Issue issue, BindingResult bindingResult,
			Model uiModel, HttpServletRequest httpServletRequest) {
		if (bindingResult.hasErrors()) {
			uiModel.addAttribute("issue", issue);
			addDateTimeFormatPatterns(uiModel);
			return "issues/create";
		}
		uiModel.asMap().clear();
		issueService.saveIssue(issue);
		issueService.sendMessage(issue);
		return "redirect:/issues/"
				+ encodeUrlPathSegment(issue.getId().toString(),
						httpServletRequest);
	}

	@RequestMapping(method = RequestMethod.PUT)
	public String update(@Valid Issue issue, BindingResult bindingResult,
			Model uiModel, HttpServletRequest httpServletRequest) {
		if (bindingResult.hasErrors()) {
			uiModel.addAttribute("issue", issue);
			addDateTimeFormatPatterns(uiModel);
			return "issues/update";
		}
		uiModel.asMap().clear();
		issueService.updateIssue(issue);
		issueService.sendMessage(issue);
		return "redirect:/issues/"
				+ encodeUrlPathSegment(issue.getId().toString(),
						httpServletRequest);
	}

	@RequestMapping(value = "/{id}", params = "form", method = RequestMethod.GET)
	public String updateForm(@PathVariable("id") Long id, Model uiModel,
			HttpServletRequest httpServletRequest) {
		Issue issue = issueService.findIssue(id);
		uiModel.addAttribute("issue", issue);
		uiModel.addAttribute("issuestatuses", issueStatusService
				.findValidIssueStatuses(issue,
						httpServletRequest.getUserPrincipal()));
		addDateTimeFormatPatterns(uiModel);
		return "issues/update";
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(params = "form", method = RequestMethod.GET)
	public String createForm(Model uiModel) {
		uiModel.addAttribute("issue", new Issue());
		uiModel.addAttribute("issuestatuses",
				issueStatusService.findDefaultIssueStatuses());
		addDateTimeFormatPatterns(uiModel);
		List dependencies = new ArrayList();
		if (trackerService.countAllTrackers() == 0) {
			dependencies.add(new String[] { "tracker", "trackers" });
		}
		if (projectService.countAllProjects() == 0) {
			dependencies.add(new String[] { "project", "projects" });
		}
		if (projectMemberService.countAllProjectMembers() == 0) {
			dependencies
					.add(new String[] { "projectmember", "projectmembers" });
		}
		if (priorityService.countAllPrioritys() == 0) {
			dependencies.add(new String[] { "priority", "prioritys" });
		}
		if (projectMemberService.countAllProjectMembers() == 0) {
			dependencies
					.add(new String[] { "projectmember", "projectmembers" });
		}
		uiModel.addAttribute("dependencies", dependencies);
		return "issues/create";
	}
}
